
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       sensor_rf_backend_serial.c
  * @author     baiyang
  * @date       2023-2-9
  ******************************************************************************
  */

/*----------------------------------include-----------------------------------*/
#include "sensor_rf_backend_serial.h"
/*-----------------------------------macro------------------------------------*/

/*----------------------------------typedef-----------------------------------*/

/*---------------------------------prototype----------------------------------*/
static void init_serial(sensor_rf_backend_t rf_backend, uint8_t serial_instance);
static void update(sensor_rf_backend_t rf_backend);
/*----------------------------------variable----------------------------------*/
static struct sensor_rf_backend_ops ops_backend = {.rf_backend_destructor  = NULL,
                                                   .update      = update,
                                                   .init_serial = init_serial,
                                                   .handle_msg  = NULL,
                                                   .max_distance_cm = NULL,
                                                   .min_distance_cm = NULL,
                                                   .get_temp    = NULL,
                                                   .get_signal_quality_pct = NULL,
                                                   ._get_mav_distance_sensor_type = NULL};
/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
void sensor_rf_backend_serial_ctor(sensor_rf_backend_ser_t rf_backend_ser,
                                    sensor_rf_backend_ser_ops_t _ops_ser,
                                    sensor_rf_backend_ops_t _ops,
                                    sensor_rf_state_t _state, sensor_rf_params_t _params,
                                    const char *name)
{
    if (_ops == NULL) {
        _ops = &ops_backend;
    }

    sensor_rf_bind_backend_function(_ops, &ops_backend);
    sensor_rf_backend_ctor(&rf_backend_ser->backend, _ops, _state, _params, name);

    rf_backend_ser->ops_ser = _ops_ser;
    rf_backend_ser->uart    = NULL;
}

void sensor_rf_bind_backend_function(sensor_rf_backend_ops_t ops_frontend, sensor_rf_backend_ops_t ops_backend)
{
    if (ops_frontend == ops_backend) {
        return;
    }

    if (ops_frontend->update == NULL && ops_backend->update != NULL) {
        ops_frontend->update = ops_backend->update;
    }

    if (ops_frontend->init_serial == NULL && ops_backend->init_serial != NULL) {
        ops_frontend->init_serial = ops_backend->init_serial;
    }
}

static void init_serial(sensor_rf_backend_t rf_backend, uint8_t serial_instance)
{
    ((sensor_rf_backend_ser_t)rf_backend)->uart = SerialManager_find_protocol_instance(SerialProtocol_Rangefinder, serial_instance);
#if 0
    if (((sensor_rf_backend_ser_const_t)rf_backend)->uart != NULL) {
        ((sensor_rf_backend_ser_const_t)rf_backend)->uart SerialManager_begin(initial_baudrate(serial_instance), rx_bufsize(), tx_bufsize());
    }
#endif
}

/*
   update the state of the sensor
*/
static void update(sensor_rf_backend_t rf_backend)
{
    if (sensor_rf_get_reading(rf_backend, &rf_backend->state->distance_m)) {
        // update range_valid state based on distance measured
        rf_backend->state->last_reading_ms = time_millis();
        sensor_rf_update_status(rf_backend);
    } else if (time_millis() - rf_backend->state->last_reading_ms > sensor_rf_read_timeout_ms(rf_backend)) {
        sensor_rf_set_status(rf_backend, RangeFinderNoData);
    }
}

/// 
uint32_t sensor_rf_InitialBaudrate(sensor_rf_backend_const_t rf_backend, uint8_t serial_instance)
{
    return SerialManager_find_baudrate(SerialProtocol_Rangefinder, serial_instance);
}
/*------------------------------------test------------------------------------*/


